package com.teradyne.merlin2.ui.client.gin;

import java.util.logging.Logger;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.teradyne.merlin2.ui.client.AppController;
import com.teradyne.merlin2.ui.client.AppControllerImpl;
import com.teradyne.merlin2.ui.client.activities.OrderEntryActivity;
import com.teradyne.merlin2.ui.client.activities.OrderListActivity;
import com.teradyne.merlin2.ui.client.activities.OrderMenuActivity;
import com.teradyne.merlin2.ui.client.activities.QuoteListActivity;
import com.teradyne.merlin2.ui.client.activities.QuoteMenuActivity;
import com.teradyne.merlin2.ui.client.activities.ServicesMenuActivity;
import com.teradyne.merlin2.ui.client.activities.TopMenuActivity;
import com.teradyne.merlin2.ui.client.activities.WorkListMenuActivity;
import com.teradyne.merlin2.ui.client.activitymappers.BottomActivityMapper;
import com.teradyne.merlin2.ui.client.activitymappers.CenterActivityMapper;
import com.teradyne.merlin2.ui.client.activitymappers.LeftSideActivityMapper;
import com.teradyne.merlin2.ui.client.activitymappers.RightSideActivityMapper;
import com.teradyne.merlin2.ui.client.activitymappers.TopActivityMapper;
import com.teradyne.merlin2.ui.client.locale.AppLocale;
import com.teradyne.merlin2.ui.client.places.AppPlaceHistoryMapper;
import com.teradyne.merlin2.ui.client.providers.OrderEntryActivityProvider;
import com.teradyne.merlin2.ui.client.providers.OrderListActivityProvider;
import com.teradyne.merlin2.ui.client.providers.OrderMenuActivityProvider;
import com.teradyne.merlin2.ui.client.providers.QuoteListActivityProvider;
import com.teradyne.merlin2.ui.client.providers.QuoteMenuActivityProvider;
import com.teradyne.merlin2.ui.client.providers.ServicesMenuActivityProvider;
import com.teradyne.merlin2.ui.client.providers.TopMenuActivityProvider;
import com.teradyne.merlin2.ui.client.providers.WorkListMenuActivityProvider;
import com.teradyne.merlin2.ui.client.resources.AppResources;
import com.teradyne.merlin2.ui.client.resources.MerlinStyle;
import com.teradyne.merlin2.ui.client.service.AsyncCallbackCustom;
import com.teradyne.merlin2.ui.client.views.AppLayout;
import com.teradyne.merlin2.ui.client.views.AppLayoutImpl;
import com.teradyne.merlin2.ui.client.views.OrderEntryView;
import com.teradyne.merlin2.ui.client.views.OrderEntryViewImpl;
import com.teradyne.merlin2.ui.client.views.OrderListView;
import com.teradyne.merlin2.ui.client.views.OrderListViewImpl;
import com.teradyne.merlin2.ui.client.views.OrderMenuView;
import com.teradyne.merlin2.ui.client.views.OrderMenuViewImpl;
import com.teradyne.merlin2.ui.client.views.QuoteListView;
import com.teradyne.merlin2.ui.client.views.QuoteListViewImpl;
import com.teradyne.merlin2.ui.client.views.QuoteMenuView;
import com.teradyne.merlin2.ui.client.views.QuoteMenuViewImpl;
import com.teradyne.merlin2.ui.client.views.ServicesMenuView;
import com.teradyne.merlin2.ui.client.views.ServicesMenuViewImpl;
import com.teradyne.merlin2.ui.client.views.TopMenuView;
import com.teradyne.merlin2.ui.client.views.TopMenuViewImpl;
import com.teradyne.merlin2.ui.client.views.WorkListMenuView;
import com.teradyne.merlin2.ui.client.views.WorkListMenuViewImpl;

public class GinModule extends AbstractGinModule {
	public static Logger logger = Logger.getLogger(GinModule.class.getName());

	@Override
	protected void configure() {
		// Views
		bind(AppLayout.class).to(AppLayoutImpl.class).in(Singleton.class);
		bind(TopMenuView.class).to(TopMenuViewImpl.class).in(Singleton.class);

		bind(OrderMenuView.class).to(OrderMenuViewImpl.class).in(Singleton.class);
		bind(OrderListView.class).to(OrderListViewImpl.class).in(Singleton.class);
		bind(OrderEntryView.class).to(OrderEntryViewImpl.class).in(Singleton.class);

		bind(QuoteMenuView.class).to(QuoteMenuViewImpl.class).in(Singleton.class);
		bind(QuoteListView.class).to(QuoteListViewImpl.class).in(Singleton.class);
		
		bind(ServicesMenuView.class).to(ServicesMenuViewImpl.class).in(Singleton.class);
		
		bind(WorkListMenuView.class).to(WorkListMenuViewImpl.class).in(Singleton.class);

		// Activities
		bind(TopMenuActivity.class).toProvider(TopMenuActivityProvider.class).in(Singleton.class);

		bind(OrderMenuActivity.class).toProvider(OrderMenuActivityProvider.class).in(Singleton.class);
		bind(OrderListActivity.class).toProvider(OrderListActivityProvider.class).in(Singleton.class);
		bind(OrderEntryActivity.class).toProvider(OrderEntryActivityProvider.class).in(Singleton.class);

		bind(QuoteMenuActivity.class).toProvider(QuoteMenuActivityProvider.class).in(Singleton.class);
		bind(QuoteListActivity.class).toProvider(QuoteListActivityProvider.class).in(Singleton.class);
		
		bind(ServicesMenuActivity.class).toProvider(ServicesMenuActivityProvider.class).in(Singleton.class);
		
		bind(WorkListMenuActivity.class).toProvider(WorkListMenuActivityProvider.class).in(Singleton.class);

		// Places
		bind(PlaceHistoryMapper.class).to(AppPlaceHistoryMapper.class).in(Singleton.class);

		// Application EventBus
		bind(EventBus.class).to(SimpleEventBus.class).in(Singleton.class);

		// Application Controller
		bind(AppController.class).to(AppControllerImpl.class).in(Singleton.class);

		// Internationalization
		bind(AppLocale.class).in(Singleton.class);

		// Resources
		bind(AppResources.class).in(Singleton.class);
		bind(SimplePager.Resources.class).in(Singleton.class);
		
		//RPC
		bind(AsyncCallback.class).to(AsyncCallbackCustom.class).in(Singleton.class);
	}

	// CSS Style Injection
	@Provides
	@Singleton
	MerlinStyle createStyle(final AppResources resources) {
		MerlinStyle style = resources.merlinStyle();
		style.ensureInjected();
		return style;
	}

	@SuppressWarnings("deprecation")
	@Provides
	@Singleton
	public PlaceController getPlaceController(EventBus eventBus) {
		return new PlaceController(eventBus);
	}

	@Provides
	@Singleton
	public PlaceHistoryHandler getHistoryHandler(PlaceController placeController, PlaceHistoryMapper historyMapper, EventBus eventBus) {
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
		return historyHandler;
	}

	@Provides
	@Singleton
	@Named("TopRegion")
	public ActivityManager getTopRegionActivityMapper(TopActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	@Named("BottomRegion")
	public ActivityManager getBottomRegionActivityMapper(BottomActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	@Named("LeftSideRegion")
	public ActivityManager getLeftSideRegionActivityMapper(LeftSideActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	@Named("RightSideRegion")
	public ActivityManager getRightSideRegionActivityMapper(RightSideActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

	@Provides
	@Singleton
	@Named("CenterContentRegion")
	public ActivityManager getMainTopContentRegionActivityMapper(CenterActivityMapper activityMapper, EventBus eventBus) {
		return new ActivityManager(activityMapper, eventBus);
	}

}
